//
// Created by 刘洋 on 2024/2/12.
//

#ifndef FOC4_FOC_H
#define FOC4_FOC_H
#include "foc_utils.h"
#include "math.h"
#include "stdlib.h"
#include "tim.h"
#include "AS5600.h"
#include "retarget.h"
#define LOW 0
#define HIGH 1
typedef struct
{
    GPIO_TypeDef *GPIOx;
    uint16_t GPIO_Pin;
}MotorPIN;
typedef struct
{
    TIM_HandleTypeDef htim;
    uint8_t Channel1;
    uint8_t Channel2;
}StepMotorPWM;
typedef struct
{
    TIM_HandleTypeDef htim;
    uint8_t Channel1;
    uint8_t Channel2;
    uint8_t Channel3;
}BLDCMotorPWM;

typedef enum {
    SinePWM,
    SpaceVectorPWM,
}FOCMode;
extern int PP;
extern int DIR;
extern uint8_t foc_modulation;
extern float voltage_power_supply;
/**
 * 无刷电机FOC实现函数，包含SinePWM和SpaceVectorPWM输出
 * @param Uq
 * @param angle_el
 */
void setPhaseVoltage(float Uq, float angle_el);
/**
 * 无刷电机的PWM设置
 * @param Ua
 * @param Ub
 * @param Uc
 */
void setBLDCPWM(float Ua,float Ub,float Uc);
/**
 * 传感器校准
 * @param _PP
 * @param _DIR
 */
void alignSensor(int _PP,int _DIR);
/**
 * 设置电源电压
 * @param voltage
 */
void setVbus(float voltage);
/**
 * 设置FOC模式：SinePWM，SpaceVectorPWM
 * @param mode
 */
void setBLDCFOCMode(FOCMode mode);
/**
 * 步进电机FOC算法
 * @param Uq
 * @param angle_el
 */
void stepMotorFOC(float Uq, float angle_el);
/**
 * 步进电机设置PWM
 * @param Ualpha
 * @param Ubeta
 */
void setStepPWM(float Ualpha, float Ubeta);
/**
 * 设置步进电机引脚，PWM输出引脚
 * @param _in1a
 * @param int1a
 * @param _in1b
 * @param int1b
 * @param _in2a
 * @param int2a
 * @param _in2b
 * @param int2b
 * @param tim
 * @param channel1
 * @param channel2
 */
void setStepMotor(GPIO_TypeDef *_in1a ,uint16_t int1a,
                     GPIO_TypeDef *_in1b ,uint16_t int1b,
                     GPIO_TypeDef *_in2a ,uint16_t int2a,
                     GPIO_TypeDef *_in2b ,uint16_t int2b,
                     TIM_HandleTypeDef tim,uint8_t channel1,uint8_t channel2);
/**
 * BLDC电机PWM输出引脚初始化
 * @param tim
 * @param channel1
 * @param channel2
 * @param channel3
 */
void setBLDCMotor(TIM_HandleTypeDef tim,uint8_t channel1,uint8_t channel2,uint8_t channel3);
#endif //FOC4_FOC_H
